package cn.gorit.system.service.impl;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.enums.StateEnums;
import cn.gorit.common.exception.SystemException;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.system.TimeLineDto;
import cn.gorit.entity.system.TimeLine;
import cn.gorit.mapper.system.TimeLineMapper;
import cn.gorit.system.service.TimeLineService;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Classname TimeLineServiceImpl
 * @Description 时间轴实现类
 * @Author CodingGorit
 * @Created on 2021/4/11 14:37
 * @IDLE IntelliJ IDEA
 **/
@Service
public class TimeLineServiceImpl implements TimeLineService {

    @Autowired
    private TimeLineMapper mapper;

    @Override
    public Result<Object> addTimeLine(TimeLineDto dto) {
        mapper.addTimeLine(dto);
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg(),"");
    }

    @Override
    public Result<Object> enableTimeLine(Integer id, Integer enable) {
        String msg = "";
        if (enable.equals(1)) {
            mapper.updateTimeLineEnabled(id, StateEnums.ENABLED.getCode());
            msg = StateEnums.ENABLED.getMsg();
        }
        if (enable.equals(0)) {
            mapper.updateTimeLineEnabled(id,StateEnums.DISABLE.getCode());
            msg = StateEnums.DISABLE.getMsg();
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),msg,"");
    }

    @Override
    public Result<Object> updateTimeLine(TimeLine dto) {
        // 参数校验
        if (dto.getVersion() == null)
            throw new SystemException(ResultEnums.UPDATE_FAILED.getCode(),ResultEnums.UPDATE_FAILED.getMsg());
        mapper.updateTimeLine(dto);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }

    // 后台分页
    @Override
    public Result<Page<TimeLine>> getTimeLineByPage(Page<TimeLine> page) {
        List<TimeLine> list = mapper.getTimeLineByPage(page);
        page.setList(list);
        int count = getTimeLineByPageCount(page);
        page.setTotalCount(count);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    @Override
    public Integer getTimeLineByPageCount(Page<TimeLine> page) {
        return mapper.getTimeLineCountByPage(page);
    }

    // 前台查询 TimeLine
    @Override
    public Result<Page<TimeLineDto>> getTimeLineDtoList(Page<TimeLineDto> page) {
        List<TimeLineDto> list = mapper.getTimeLineList(page);
        page.setList(list);
        page.setTotalCount(getTimeLineDtoCount(page));
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    // 前台
    @Override
    public Integer getTimeLineDtoCount(Page<TimeLineDto> page) {
        return mapper.getTimeLineCount(page);
    }
}
